﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;
using NetBay.Core.BusinessEntities;
using System.Net;
#endregion

namespace NetBay.Core.Mail.SMTP.Assync 
{
    public class Smtp
    {
        public static ReturnBoolean SendMail(string p_szSmtpServer, string p_szSubject, string p_szBody, MailAddress p_oMailAdressFrom, IEnumerable<MailAddress> p_lstMailAdressTo, List<string> p_oFullPathFilesName = null, List<string> p_oOriginalFileName = null, string login = "", string password = "")
        {
            ReturnBoolean l_oReturnObject = new ReturnBoolean { Value = true };
            try
            {
                // Command line argument must the the SMTP host.
                SmtpClient l_oClient = new SmtpClient(p_szSmtpServer);


                //login = "fpetit@netbay.mc";
                //password = "Netbay@007";


                if (!string.IsNullOrEmpty(login) && !string.IsNullOrEmpty(password))
                    l_oClient.Credentials = new NetworkCredential(login, password);

                // Specify the message content.
                MailMessage l_oMessage = new MailMessage { Body = p_szBody };
                l_oMessage.IsBodyHtml = true;
                foreach (MailAddress l_oToAdress in p_lstMailAdressTo)
                {
                    l_oMessage.To.Add(l_oToAdress);
                }
                l_oMessage.From = p_oMailAdressFrom;

                // Include some non-ASCII characters in body and subject.
                l_oMessage.Body += Environment.NewLine;
                l_oMessage.BodyEncoding = System.Text.Encoding.UTF8;
                l_oMessage.Subject = p_szSubject;
                l_oMessage.SubjectEncoding = System.Text.Encoding.UTF8;

                // Add attachments if needed
                if ((p_oFullPathFilesName != null) &&
                    (p_oOriginalFileName != null)
                    )
                {
                    // Identical number of items
                    if (p_oFullPathFilesName.Count == p_oOriginalFileName.Count)
                    {
                        // Any file to attach?
                        if (p_oFullPathFilesName.Count > 0)
                        {
                            List<string>.Enumerator l_oEnumFullPath = p_oFullPathFilesName.GetEnumerator();
                            List<string>.Enumerator l_oEnumOriginalName = p_oOriginalFileName.GetEnumerator();
                            // Loop for all attachment
                            foreach (string t in p_oOriginalFileName)
                            {
                                // Move to next values
                                if (l_oEnumFullPath.MoveNext() == false)
                                    break;
                                if (l_oEnumOriginalName.MoveNext() == false)
                                    break;

                                // Open the file at the beginning to make the attachment.
                                FileStream l_ofs = new FileStream(l_oEnumFullPath.Current, FileMode.Open, FileAccess.Read);
                                // Make a contentType 
                                ContentType l_ict = new ContentType
                                                        {
                                                            MediaType = MediaTypeNames.Application.Octet,
                                                            Name = l_oEnumOriginalName.Current
                                                        };
                                // Attach the file stream to the e-mail message.
                                Attachment l_oAttachment = new Attachment(l_ofs, l_ict);
                                l_oMessage.Attachments.Add(l_oAttachment);
                            }
                        }
                    }
                }

                // Set the method that is called back when the send operation ends.
                l_oClient.Send(l_oMessage);

                l_oMessage.Dispose();
            }
            catch (Exception ex)
            {
                l_oReturnObject.ApplicationException = ex;
                l_oReturnObject.ErrorMessage = ex.Message;
                l_oReturnObject.Value = false;
            }

            return l_oReturnObject;
        }

        /// <summary>
        /// Sends the mail.
        /// </summary>
        /// <param name="p_szSmtpServer">The SMTP server.</param>
        /// <param name="p_szSubject">The subject.</param>
        /// <param name="p_szBody">The body.</param>
        /// <param name="p_oMailAdressFrom">The mail adress from.</param>
        /// <param name="p_lstMailAdressTo">The mail adress to.</param>
        /// <returns></returns>
        public static ReturnBoolean SendMail(string p_szSmtpServer, string p_szSubject, string p_szBody, MailAddress p_oMailAdressFrom, MailAddress p_lstMailAdressTo, string login = "", string password = "")
        {
            return SendMail(p_szSmtpServer, p_szSubject, p_szBody, p_oMailAdressFrom, new List<MailAddress>() { p_lstMailAdressTo }, null, null, login, password);
        }

    }
}
